<template>
	<view class="content">
        <!-- navbar -->
        <u-navbar :fixed="true" :safeAreaInsetTop="true" :placeholder="true" :shadow="true"
            @leftClick="back" title="选择班组" >
        </u-navbar>

        <!-- 内容 -->
        <view class="uni-area">
            <!-- 列表 -->
            <uni-item-team-select class="u-m-t-30" v-for="(tem,ind) in page.list" :key="ind" :listItem="tem" @selectGroup="selectGroup"/>

            <!-- 返回顶部 -->
            <u-back-top :scroll-top="page.scrollTop" @tap="toTop"></u-back-top>
            <!-- 加载更多 -->
            <u-loadmore :status="page.status" />
        </view>
	</view>
</template>

<script>    
import {mapGetters,mapMutations} from 'vuex'
export default {
    data() {
        return {
            systemInfo: uni.getStorageSync('systemInfo'), // 设备信息
            type: null,  // 类型
            parameter:{ // 参数
                deptId: uni.getStorageSync("userInfo").deptId, // 所属部门编号
                gcbh: uni.getStorageSync("userInfo").ssgcbh // 所属项目(工程)编号
            },
            // 分页相关参数
            paging:{
                pageNum: 1,
                pageSize: this.$config.pageSize,
                keyWord: '',
            },
            page:{
                scrollTop: 0,
                status: 'nomore',
                total: 0,
                list: [], // 列表
            },
        }
    },

    onPageScroll(e) {
        this.page.scrollTop = e.scrollTop;
    },
    onReachBottom(){
        if( this.page.list.length < this.page.total ){
            this.paging.pageNum += 1
            this.page.status = 'loading'
            this.getGroupList()
        }else{
            this.page.status = 'nomore'
        }
    },

    onLoad(option) {
        // 班组列表
        this.type = option.type
        this.getGroupList()
    },

    methods: {
        ...mapMutations(['setTransferGroup','setSelectTransferGroup','setForemanGroup']),

        // 返回
        back(){
            uni.navigateBack()
        },
        // 长按事件
        longTap(value){
            uni.$u.toast(value)
        },



        // 班组列表
        getGroupList(){
            this.page.status = 'loading'
            const parameter = Object.assign(this.parameter,this.paging)
            this.$request(this,this.$apis.GroupList,parameter).then(res=>{
                // console.log('班组列表',res.data)
                this.page.total = res.total
                if( res.data.length  ){
                    this.page.status = 'loadmore'
                    res.data.map((v,i)=>{
                        this.page.list.push(v)
                    })
                }else{
                    this.page.status = 'nomore'
                    uni.$u.toast('暂时未查询到更多数据')
                }
            }).then()
        },
        // 选择班组
        selectGroup(e){
            switch (this.type) {
                case 'transfer1':
                    this.setTransferGroup(e.data)
                    break;
                case 'transfer2':
                    this.setSelectTransferGroup(e.data)
                    break;
                case 'foreman':
                    this.setForemanGroup(e.data)
                    break;
            }
            uni.navigateBack()
        },

        // 回到顶部
        toTop(){
            uni.pageScrollTo({ scrollTop: 0, duration: 300 })
        }
    }
}
</script>

<style lang="scss">
    // .content{}
</style>
